package view.panel.result
{
	import control.panel.ResultPanelControl;
	
	import flash.text.TextField;
	
	import model.result.ResultData;
	import model.result.RoundResultData;
	
	import utils.DisplayTool;
	import utils.TextTool;
	
	import view.panel.AbsPanel;
	import global.GameState;
	
	public class ResultPanel extends AbsPanel
	{
		private var resultItems:Array;
		private var titleName:TextField;
		private var titleMoney:TextField;
		private var autoClose:TextField;
		
		
		public function ResultPanel(panelControl:ResultPanelControl)
		{
			super(panelControl);
			defaultX = 275;
			defaultY = 120;
			initCloseBut();
			resetBg(230,162,.6);
			resetCloseBut();	
			initResultItems();
			initText();
			initTweenPanelOpen();
		}
		
		private function initText():void
		{
			titleName = TextTool.getTextField(50,20,12,0xFFFFFF);
			titleName.x = 25;
			titleName.y = 15;
			addChild(titleName);
			
			titleMoney = TextTool.getTextField(50,20,12,0xFFFFFF);
			titleMoney.x = 120;
			titleMoney.y = 15;
			addChild(titleMoney);
			
			autoClose = TextTool.getTextField(100,20,12,0xFFFFFF);
			
			autoClose.x = 70;
			
			
			titleMoney.text = "金币";
			titleName.text = "玩家";
			
			drawLine();
		}
		
		private function drawLine():void
		{
			with (this.graphics)
			{
				lineStyle(1,0);
				moveTo(20,32)
				lineTo(220,32);
			}
		}
		
		private function initResultItems():void
		{
			resultItems = [];
			var resultItem:ResultItem;
			for (var i:int=0; i<6; i++)
			{
				resultItem = new ResultItem();
				resultItem.x = 25;
				resultItems.push(resultItem);
			}
		}
		
		public function updateResult(roundResultData:RoundResultData):void
		{
			clear();
			if (roundResultData)
			{
				var resultData:ResultData;
				var resultItem:ResultItem;
				for (var i:int=0; i<roundResultData.resultDatas.length; i++)
				{
					resultData = roundResultData.resultDatas[i];
					resultItem = resultItems[i];
					resultItem.y = 35 + i * 20;
					resultItem.update(resultData);
					addChild(resultItem);
					
					if (resultData.sitId == GameState.playerCurrentSitId)
					{
						resultItem.hint();
					}
				}
				resetBg(230,55 + i* 20,.7); 
				drawLine();
				
			}
		}
		
		private function setYCenter():void
		{
			defaultY = (430 - this.height ) * .5;
			autoClose.y = this.height - 20;
			addChild(autoClose);
		}
		
		public function clear():void
		{
			var resultItem:ResultItem;
			for (var i:String in resultItems)
			{
				resultItem = resultItems[i];
				resultItem.clear();
				DisplayTool.removeChild(this,resultItem);
			}
			DisplayTool.removeChild(this,autoClose);
		}
		
		public function updateAutoClose(content:String):void
		{
			autoClose.htmlText = content;
		}
		
		override public function open() : void
		{
			setYCenter();
			super.open();
			
		}
	}
}